﻿using UnityEngine;
using System.Collections;

public class Shot: MonoBehaviour
{
	public float speed;
	public float damage = 25f;
	private float TTL = 2;
	public GameObject particle;
	public Transform spawn;
	
	void Start ()
	{
		rigidbody.velocity = transform.forward * speed;
		Destroy(gameObject, TTL);
	}
	void OnCollisionEnter(Collision collision){
		if(collision.gameObject.tag == "Level"){
			Instantiate(particle, spawn.position, spawn.rotation);
			Destroy(this.gameObject);
		}

		if(collision.gameObject.tag == "Enemy"){
			Destroy(this.gameObject);
		}
		
		if(collision.gameObject.layer == LayerMask.NameToLayer("EnemyNPC"))
		{
			Health h = collision.transform.GetComponent<Health>();
			
			if (h !=null) {
				h.TakeDamage(damage, collision);
			}
			Destroy(this.gameObject);
		}

		if(collision.gameObject.tag == "Player")
		{
			Health h = collision.transform.GetComponent<Health>();
			
			if (h !=null) {
				h.TakeDamage(damage, collision);
			}
			Destroy(this.gameObject);
		}
    }
}